package com.barbeque.mall.controller;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.advertise.BO.CouponListBo;
import com.barbeque.mall.bean.advertise.BO.CouponListUserBo;
import com.barbeque.mall.bean.advertise.MarketCoupon;
import com.barbeque.mall.bean.advertise.MarketCouponUser;
import com.barbeque.mall.bean.advertise.VO.MyBaseRespVo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.service.CouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author bai
 * @since 2022/07/17 01:58
 */
@RestController
@RequestMapping("admin/coupon")
public class CouponController {
    @Autowired
    CouponService couponService;

    // localhost:8083/admin/coupon/list?page=1&limit=20&sort=add_time&order=desc
    @RequestMapping("list")
    @RequiresPermissions("admin:coupon:list")
    public BaseRespVo list(CouponListBo couponListBo){
        CommonData<MarketCoupon> marketCouponCommonData = new CommonData<>();
        if(couponListBo.getName() != null || couponListBo.getType() != null || couponListBo.getStatus() != null){
            // 做模糊查询操作
            marketCouponCommonData = couponService.queryCouponListByCondition(couponListBo);

        }else {
            // 查到广告优惠券列表
            marketCouponCommonData = couponService.queryCouponList(couponListBo);
        }

        return BaseRespVo.ok(marketCouponCommonData);
    }

    // localhost:8083/admin/coupon/create
    @RequestMapping("create")
    @RequiresPermissions("admin:coupon:create")
    public BaseRespVo create(@RequestBody MarketCoupon marketCoupon){

        marketCoupon = couponService.insertCouponInfo(marketCoupon);


        return BaseRespVo.ok(marketCoupon);
    }

    // localhost:8083/admin/coupon/update
    @RequestMapping("update")
    @RequiresPermissions("admin:coupon:update")
    public BaseRespVo update(@RequestBody MarketCoupon marketCoupon){

        marketCoupon = couponService.updateCouponInfo(marketCoupon);

        return BaseRespVo.ok(marketCoupon);
    }

    // localhost:8083/admin/coupon/delete
    @RequestMapping("delete")
    @RequiresPermissions("admin:coupon:delete")
    public MyBaseRespVo delete(@RequestBody MarketCoupon marketCoupon){
        MyBaseRespVo myBaseRespVo = new MyBaseRespVo();
        couponService.deleteCouponInfo(marketCoupon);

        myBaseRespVo.setErrmsg("成功");
        myBaseRespVo.setErrno(0);
        return myBaseRespVo;
    }

    // localhost:8083/admin/coupon/read?id=8
    @RequestMapping("read")
    @RequiresPermissions("admin:coupon:read")
    public BaseRespVo read(@RequestParam("id") Integer id){
        BaseRespVo<MarketCoupon> marketCouponBaseRespVo = new BaseRespVo<>();
        MarketCoupon marketCoupon = couponService.queryCouponInfoById(id);

        marketCouponBaseRespVo.setData(marketCoupon);
        marketCouponBaseRespVo.setErrmsg("成功");
        marketCouponBaseRespVo.setErrno(0);

        return marketCouponBaseRespVo;

    }
    // localhost:8083/admin/coupon/listuser?page=1&limit=20&couponId=2&sort=add_time&order=desc
    @RequestMapping("listuser")
    @RequiresPermissions("admin:coupon:listuser")
    public BaseRespVo listuser(CouponListUserBo couponListUserBo){
        CommonData<MarketCouponUser> marketCouponUserCommonData = new CommonData<>();

        if(couponListUserBo.getUserId() == null && couponListUserBo.getStatus() == null){
            // 根据couponId进行查询
            marketCouponUserCommonData = couponService.querryCouponListUserByCouponId(couponListUserBo);
        }else {
            // 进行优惠券持有人模糊查询
            marketCouponUserCommonData = couponService.querryCouponListUserCondition(couponListUserBo);
        }

        return BaseRespVo.ok(marketCouponUserCommonData);
    }


}
